package com.example.carRental.controller;

import com.example.carRental.controller.request.Request_AddOrder;
import com.example.carRental.entity.Car;
import com.example.carRental.entity.Customer;
import com.example.carRental.entity.Order;
import com.example.carRental.service.ChartererService;
import com.example.carRental.service.CustomerService;
import com.example.carRental.utils.R;
import com.example.carRental.utils.RD;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 92307
 * @version 1.0
 * @time 2022/3/29
 * @Describe describe
 */
@RestController
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    private ChartererService chartererService;

    /**
     * @Description: 获得自身信息
     * @Author: lpf
     * @Date: 2022/3/29
     */
    @RequestMapping("/customer/info")
    public RD getInfo(){
        return RD.success().putData("data",customerService.getInfo());
    }

    /**
     * @Description: 获得汽车信息
     * @Author: lpf
     * @Date: 2022/3/29
     */
    @RequestMapping("/customer/carList")
    public RD getAllCar(){
        List<Car> allCar = customerService.getAllCar();
        System.out.println(allCar);
        for (Car car : allCar) {
            car.setChartererName(chartererService.getNameById(car.getChartererId()));
            car.setChartererId(null);
        }
        return RD.success().putData("data",allCar);
    }

    /**
     * @Description: 新增订单
     * @Author: lpf
     * @Date: 2022/4/2
     */
    @PostMapping("/customer/addOrder")
    public R addOrder(@RequestBody Request_AddOrder request) {
        customerService.addOrder(request.getCarId(),request.getDays());
        return R.success();
    }

    /**
     * @Description: 修改自身信息
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @PostMapping("/customer/update")
    public R updateInfo(@RequestBody Customer customer){
        customerService.updateInfo(customer);
        return R.success();
    }
    /**
     * @Description: 查看订单
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @PostMapping("/customer/orderList")
    public RD getOrder(){
        List<Order> orders = customerService.orderList();
        return RD.success().putData("data",orders).putData("total",orders.size());
    }
}
